package com.cls.system.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.enums.DictEnum;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.system.entity.SysDict;
import com.cls.system.entity.SysFile;
import com.cls.system.service.ISysDictService;
import com.cls.system.service.ISysFileService;
import com.github.pagehelper.PageInfo;
import com.cls.system.entity.Template;
import com.cls.system.service.ITemplateService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 模板管理 Controller
 *
 * @author slx
 * @date 2020-11-24 15:33:34
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class TemplateController extends BaseController {

    private final ITemplateService templateService;
    private final ISysFileService sysFileService;
    private final ISysDictService sysDictService;
    /**
    * 模板管理列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "system/template")
    public String templateIndex(){
        return MyUtil.view("system/template/template");
    }

    /**
    * 模板管理新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "system/template/templateAdd")
    public String templateAdd(Model model){

        List<SysDict> templateTypeCodes = sysDictService.findByParentCode( DictEnum.TEMPLATE_TYPE_CODE.getCode());
        model.addAttribute("templateTypeCodes",templateTypeCodes);
        return MyUtil.view("system/template/templateAdd");
    }

    /**
    * 模板管理修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "system/template/templateUpdate/{templateId}")
    public String templateUpdate(@PathVariable Long templateId, Model model){
        Template template = templateService.getById(templateId);
        SysFile sysFile = sysFileService.getById(template.getFileId());
        if(sysFile!=null){
        template.setFileName(sysFile.getName());
        template.setFileUrl(sysFile.getUrl());
        }
        model.addAttribute("template",template);
        return MyUtil.view("system/template/templateUpdate");
    }

    /**
    * 模板管理详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "system/template/templateDetail/{templateId}")
    public String templateDetail(@PathVariable Long templateId, Model model){
        Template template = templateService.getById(templateId);
        SysFile sysFile = sysFileService.getById(template.getFileId());
        if(sysFile!=null) {
            template.setFileName(sysFile.getName());
            template.setFileUrl(sysFile.getUrl());
        }
        model.addAttribute("template",template);
        return MyUtil.view("system/template/templateDetail");
    }

    @GetMapping("template")
    @ResponseBody
    @RequiresPermissions("template:list")
    public ResponseVO getAllTemplates(Template template) {
        return ResponseVO.success(templateService.findTemplates(template));
    }

    @GetMapping("template/list")
    @ResponseBody
    @RequiresPermissions("template:list")
    public ResponseVO templateList(QueryRequest request, Template template) {
        PageInfo<Template> pageInfo =  this.templateService.findTemplates(request, template);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增Template", exceptionMessage = "新增Template失败")
    @PostMapping("template")
    @ResponseBody
    @RequiresPermissions("template:add")
    public ResponseVO addTemplate(@Valid Template template) {
        this.templateService.createTemplate(template);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除Template", exceptionMessage = "删除Template失败")
    @PostMapping("template/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("template:delete")
    public ResponseVO deleteTemplates(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.templateService.deleteTemplates(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个Template", exceptionMessage = "删除Template失败")
    @PostMapping("template/delete/{templateId}")
    @ResponseBody
    @RequiresPermissions("template:delete")
    public ResponseVO deleteTemplate(@PathVariable Long templateId) {
        this.templateService.removeById(templateId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改Template", exceptionMessage = "修改Template失败")
    @PostMapping("template/update")
    @ResponseBody
    @RequiresPermissions("template:update")
    public ResponseVO updateTemplate(Template template) {
        this.templateService.updateTemplate(template);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改Template", exceptionMessage = "导出Excel失败")
    @PostMapping("template/excel")
    @ResponseBody
    @RequiresPermissions("template:export")
    public void export(QueryRequest queryRequest, Template template, HttpServletResponse response) {
        List<Template> templates = this.templateService.findTemplates(queryRequest, template).getList();
        ExcelKit.$Export(Template.class, response).downXlsx(templates, false);
    }
}
